package com.shiyi.design.singleton;

/**
 * 使用内部类完成懒加载与单例
 * 原理：JVM在加载类的时候，不会加载内部类的
 * 比较完美，但是可以通过反射初始化对象
 * @Author qiu
 * @Date 2021/1/10 23:09
 */
public class SingletonDesignModel8 {

    private SingletonDesignModel8(){};

    private static class InnerSingletonDesignModel8 {
        private static volatile SingletonDesignModel8 INSTANCE = new SingletonDesignModel8();
    }

    public static SingletonDesignModel8 getInstance() {
        return InnerSingletonDesignModel8.INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Thread.currentThread().getName() + "----" + SingletonDesignModel8.getInstance().hashCode())).start();
        }
    }
}
